package chuangyi.com.org.ViewModel;

import android.content.Context;

import com.solidfire.gson.internal.LinkedTreeMap;

import java.util.ArrayList;

import chuangyi.com.org.DOMIHome.R;
import chuangyi.com.org.DOMIHome.Utils.Utils;
import chuangyi.com.org.DOMIHome.Utils.WebApi;
import chuangyi.com.org.DOMIHome.adapter.BindingListItem;
import chuangyi.com.org.DOMIHome.adapter.ListBindingAdapter;
import chuangyi.com.org.DOMIHome.BR;
import chuangyi.com.org.DOMIHome.adapter.XListAdapterInterface;

/**
 * Created by zxh on 16/12/20.
 */
public class MyFansModel extends XListModel {
    private boolean loading;

    private UserInfoModel self;
    private UserItemModel mySelfItem;

    private int uid;

    public UserInfoModel getSelf() {
        return self;
    }

    public void setSelf(UserInfoModel self) {
        this.self = self;
//        this.getMySelfItem();
    }

    public MyFansModel(Context context, int uid) {
        super(context);
        this.uid = uid;
        if (uid == User.getCurrentUser().getUid()) {
            super.getHandler().setTitle("粉丝");
        } else {
            super.getHandler().setTitle("粉丝");
            mySelfItem = null;
        }

        super.getAdapter().setVariableId(BR.user);
    }

    private void getMySelfItem() {
        if (self == null) {
            mySelfItem = null;
        }
        mySelfItem = new UserItemModel(getContext(), R.layout.i_fans);
        mySelfItem.setUid(User.getCurrentUser().getUid());
        new Thread(new Runnable() {
            @Override
            public void run() {
                self.getMoreInfo();
                mySelfItem.setNickName(self.getNickname());
                mySelfItem.setExpert(self.isExpertflag());
                mySelfItem.setAttented(false);
                mySelfItem.setParent(MyFansModel.this);
                mySelfItem.setAvatar(self.getAvatar());
                mySelfItem.setJob(self.getJob());
                mySelfItem.setCompany(self.getCompany());
                mySelfItem.setLabel(self.getIndustry());
            }
        }).start();
    }

    @Override
    public void load(final int page) {
        if (loading) {
            return;
        }
        setNoMore();
        loading = true;
        getAdapter().clear(true);
//        if (self != null) {
//            getAdapter().add(mySelfItem);
//            getAdapter().notifyDataSetChanged();
//        }
        new Thread(new Runnable() {
            @Override
            public void run() {
// uid：用户id,nickname:昵称，avatar:头像，company：公司,expertflag:是否专家,job:职位数据,
// gzflag:是否关注
                XListAdapterInterface adapter = getAdapter();
                ArrayList list = WebApi.getMyFans(getContext(), uid);
                try {
                    if (list == null) {
                        list = new ArrayList();
                    }
                    for (Object item : list) {
                        LinkedTreeMap map = (LinkedTreeMap) item;
                        UserItemModel user = new UserItemModel(getContext(), R.layout.i_fans);
                        user.setAttented(Utils.getBooleanValue(map.get("gzflag")));
                        user.setAvatar((String) map.get("avatar"));
                        user.setCompany((String) map.get("company"));
                        ArrayList jobList = (ArrayList) map.get("job");
                        if (jobList != null) {
                            for (Object job : jobList) {
                                user.setJob((String) job);
                            }
                        }
                        user.setExpert(Utils.getBooleanValue(map.get("expertflag")));
                        user.setNickName((String) map.get("nickname"));
                        user.setUid(Utils.getIntValue(map.get("uid")));
                        user.setParent(MyFansModel.this);
                        adapter.add(user);
                    }
                } catch (Exception ex) {
                    showToast("数据解析时发生错误");
                }
                notifyDataSetChanged(list);
                loading = false;
            }
        }).start();
    }

    @Override
    public void notifyListChanged(int action, final BindingListItem item) {
        if (action == ListBindingAdapter.ACTION_DEL) {
            if (self != null) {
                self.setWdgz(self.getWdgz() - 1);
            }
        } else if (action == ListBindingAdapter.ACTION_ADD) {
            if (self != null) {
                self.setWdgz(self.getWdgz() + 1);
            }
        }
    }
}
